package net.board.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

import net.board.db.BoardDAO;
import net.board.db.BoardBean;

public class BoardAddAction implements Action {
	 public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		 
		BoardDAO boarddao=new BoardDAO();
	   	BoardBean boarddata=new BoardBean();
	   	ActionForward forward=new ActionForward();
	   	
	   	int number = Integer.parseInt(request.getParameter("number"));

		String realFolder="";
   		String saveFolder="board/qnaUpload";
   		
   		int fileSize=5*1024*1024;
   		
   		realFolder=request.getRealPath(saveFolder);
   
   		boolean result=false;
   		
   		try{
   			
   			MultipartRequest multi=null;
   			
   			multi=new MultipartRequest(request,
   					realFolder,
   					fileSize,
   					"utf-8",
   					new DefaultFileRenamePolicy());
   			
   			
   			
   			boarddata.setMember_Id(boarddao.idGet(multi.getParameter("QNA_NICK")));
   			boarddata.setQNA_NICK(multi.getParameter("QNA_NICK"));
   			boarddata.setQNA_PASS(multi.getParameter("QNA_PASS"));
	   		boarddata.setQNA_SUBJECT(multi.getParameter("QNA_SUBJECT"));
	   		boarddata.setQNA_CONTENT(multi.getParameter("QNA_CONTENT"));
	   		boarddata.setQNA_FILE(
	   		multi.getFilesystemName((String)multi.getFileNames().nextElement()));
	   		
	   		result=boarddao.boardInsert(boarddata);
	   		
	   		if(result==false){
	   			return null;
	   		}
	   		
	   		forward.setRedirect(true);
	   		forward.setPath("./BoardList.bo?number="+number);
	   		return forward;
	   		
  		}catch(Exception ex){
   			ex.printStackTrace();
   		}
  		return null;
	}  	
}